package com.xinshujia.controller;

import com.xinshujia.entity.QuartzEntity;
import com.xinshujia.service.QuartzService;
import com.xinshujia.utils.ConstantsUtil;
import org.quartz.Scheduler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * *************************************
 * *   @author: 野狼上校               **
 * *   @since: 2024-07-23 16:36       **
 * *   @version: 1.0                  **
 * *************************************
 * <p>
 */
@RestController
@RequestMapping("quartz")
public class QuartzController {
    private final Scheduler scheduler;
    private final QuartzService quartzService;

    public QuartzController(Scheduler scheduler, QuartzService quartzService) {
        this.scheduler = scheduler;
        this.quartzService = quartzService;
    }

    @GetMapping("createJob")
    public String createJob(@RequestParam("deviceGuid") String deviceGuid) throws Exception {
        QuartzEntity quartzEntity = new QuartzEntity();
        quartzEntity.setJobName(deviceGuid);
        quartzEntity.setJobClass("com.xinshujia.task.QuartzTask");
        quartzEntity.setStatus(ConstantsUtil.QUARTZ_STATUS_RUNNING);
        quartzEntity.setCron("0/5 * * * * ?");
        boolean jobFlag = quartzService.createJob(scheduler, quartzEntity);
        if (jobFlag) {
            return "创建成功";
        } else {
            return "创建失败";
        }
    }

    @GetMapping("pauseJob")
    public String pauseJob(@RequestParam("deviceGuid") String deviceGuid) {
        boolean jobFlag = quartzService.pauseJob(scheduler, deviceGuid);
        if (jobFlag) {
            return "暂停成功";
        } else {
            return "暂停失败";
        }
    }
}
